<template>
  <div id="app">
    <!-- <div v-show="isLoading">加载中</div> -->
    <router-view></router-view>
    <!--<footers></footers>-->
  </div>
</template>

<script>
  import store from '@/vuex/store'
  import {mapState} from 'vuex'
  import loca from '@/common/js/base/location.js'
  //import footers from '@/components/footer'
  export default {
    name: 'app',
    data() {
      return {
        mypoi: null,
      }
    },
    computed: {//加载中状态
      isLoading() {
        return store.state.isLoading
      },
      mylocation() {
        return store.state.watchPosition
      }
    },
    watch: {
      isLoading(newVal, olVal) {
        if (newVal == false) {
          this.$dialog.loading.close();
        } else if (newVal == true) {
          this.$dialog.loading.open('加载中，请稍后');
        }
      },
      mylocation: {//监听位置变化
        handler(newValue, oldValue) {
          let _this=this
                setTimeout(() => {
              //  console.log(newValue.lng+','+oldValue.lng);
                if (store.state.loginStatus == 0) {//如果登录成功，向服务器发送位置
                let serPOi = {//获取服务器位置
                  userId: localStorage.userid
                }
                //  alert(1)
                _this.showPosition(serPOi)

                  let data = {//创建新坐标变量
                    userId: localStorage.userid,
                    lat: newValue.lat,
                    lng: newValue.lng,
                    province: newValue.province,
                    city: newValue.city,
                    region: newValue.district,
                    detail: newValue.street + "," + newValue.streetNum
                  }
                  // console.log(data.city)
                  let selfpoi = _this.mypoi;//保存自己坐标
                  if (selfpoi != null) {
                    // alert(selfpoi.lng+','+selfpoi.lat)
                    console.log(selfpoi)
                    console.log(newValue)
                    let distance = loca.getDisance(newValue.lat, newValue.lng, selfpoi.lat, selfpoi.lng)//获取两次坐标距离
                  console.log(distance)
                    if (distance >= 30) {//如果距离大于10米
                      // alert("哈哈哈")
                          _this.send(data)
                    }
                  } else if (selfpoi == null) {//如果
                    // alert("哈哈哈a啊")
                    _this.send(data)
                  }
                }            
          }, 3000);
        },
        deep: true
      }
    },
    methods: {
      send(poi) {//发送坐标
        this.ylAjax.sendPoi(poi)
          .then(res => {
            // console.log(res)
          })
      },
      showPosition(poi) {//查看位置
        this.ylAjax.showPoi(poi)
          .then(res=>{
            if(res.state==0){
              this.mypoi=res.data
              //  console.log(this.mypoi)
            }
          })
      }
    },
    mounted() {
      this.$store.commit('loginStatus',1)
      // let data = {
      //   userId: localStorage.userid
      // }
//      this.showPosition(data)
      // alert("1"+mylocation.lng)
    }
  }
</script>

<style>

</style>
